package com.app.babygo.help;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.app.babygo.obj.AchievementTask;
import com.app.babygo.obj.Child;
import com.app.babygo.obj.User;

public class SqlServerHelper {

	public void SycServer(final User acountUser, final ArrayList<Child> children, final ArrayList<AchievementTask> achievements) {
		Thread t = new Thread(new Runnable() {
			public void run() {
				try {
					//sync functions
					addAccountToDB(acountUser);
					addChildToDB(children);
					addAchievementsToDB(achievements);
					
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			};
		});
		t.start();
	}
	
	public void SycServer(final Child children) {
		Thread t = new Thread(new Runnable() {
			public void run() {
				try {
					//sync functions
					addChildToDB(children);
					
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			};
		});
		t.start();
	}
	
	public void SycServer(final User acountUser) {
		Thread t = new Thread(new Runnable() {
			public void run() {
				try {
					//sync functions
					addAccountToDB(acountUser);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			};
		});
		t.start();
	}
	
	private static Connection getconnection() throws SQLException {
		Connection conn = null;
		try {
			// http://www.devdaily.com/java/edu/JDBC-SQLProcessor/Simple_JDBC_Example.shtml
			// The newInstance() call is a work around for some
			// broken Java implementations
			Class.forName("com.mysql.jdbc.Driver").newInstance();
		} catch (Exception ex) { // handle the error
			ex.printStackTrace();
		}

		conn = DriverManager
				.getConnection("jdbc:mysql://172.25.99.214/babygo?"
						+ "user=root&password=haha13");// VancouverTongue,yiyeh

		return conn;
	}
	
	 private void addAccountToDB(User acountUser) throws SQLException {
         Connection conn = getconnection();
         
         Statement statement = conn.createStatement();
         String d = "insert into account(userid,username,password) values( "
                         + '"'
                         + acountUser.getUserid()
                         + '"'
                         + " , "
                         + '"'
                         + acountUser.getUsername()
                         + '"'
                         + " , "
                         + '"'
                         + acountUser.getPassword()
                         + '"'
                         + ")";
         
         boolean insert_right = statement.execute(d);
	         conn.close();
	 }
	 private void addChildToDB(ArrayList<Child> allChildren) throws SQLException {
         Connection conn = getconnection();
         
         for (int i = 0; i < allChildren.size(); i++)
         {
        	 Statement statement = conn.createStatement();
             String d = "insert into child(id, parentid, name, age, gender, info, pic_path) values( "
                             + '"'
                             + allChildren.get(i).getCid()
                             + '"'
                             + " , "
                             + '"'
                             + allChildren.get(i).getChildparentid()
                             + '"'
                             + " , "
                             + '"'
                             + allChildren.get(i).getChildname()
                             + '"'
                             + " , "
                             + '"'
                             + allChildren.get(i).getChildage()
                             + '"'
                             + " , "
                             + '"'
                             + allChildren.get(i).getChildgender()
                             + '"'
                             + " , "
                             + '"'
                             + allChildren.get(i).getChildinfo()
                             + '"'
                             + " , "
                             + '"'
                             + allChildren.get(i).getChildPicPath()
                             + '"'
                             + ")";
             
             boolean insert_right = statement.execute(d);
    	         conn.close();
         }
         
	 }
	 public void addChildToDB(Child child) throws SQLException {
         Connection conn = getconnection();
         
    	 Statement statement = conn.createStatement();
         String d = "insert into child(id, parentid, name, age, gender, info, pic_path) values( "
                         + '"'
                         + child.getCid()
                         + '"'
                         + " , "
                         + '"'
                         + child.getChildparentid()
                         + '"'
                         + " , "
                         + '"'
                         + child.getChildname()
                         + '"'
                         + " , "
                         + '"'
                         + child.getChildage()
                         + '"'
                         + " , "
                         + '"'
                         + child.getChildgender()
                         + '"'
                         + " , "
                         + '"'
                         + child.getChildinfo()
                         + '"'
                         + " , "
                         + '"'
                         + child.getChildPicPath()
                         + '"'
                         + ")";
         
         boolean insert_right = statement.execute(d);
	         conn.close();         
	 }
	 private void addAchievementsToDB(ArrayList<AchievementTask> allAchievement) throws SQLException {
         Connection conn = getconnection();
         
         for (int i = 0; i < allAchievement.size(); i++)
         {
        	 Statement statement = conn.createStatement();
             String d = "insert into child_achievement(id, name, info) values( "
                             + '"'
                             + allAchievement.get(i).getTaskid()
                             + '"'
                             + " , "
                             + '"'
                             + allAchievement.get(i).getTaskname()
                             + '"'
                             + " , "
                             + '"'
                             + allAchievement.get(i).getTaskdescription()
                             + '"'
                             + ")";
             
             boolean insert_right = statement.execute(d);
    	         conn.close();
         }
         
	 }
}
